<template>
  <div class="big_container">
    <div class="clo_container">
      <span class="iconfont iconicon-test"></span>
    </div>
    <div class="logo_container">
      <span class="iconfont iconnew"></span>
    </div>
    <!--用户名登录组件-->
    <myinput
      msg="用户名/手机号码"
      type="text"
      :proof="/\d{3,9}/"
      errmsg="请输入正确用户名"
      @getval="setusername"
    ></myinput>
    <!--密码登录组件-->
    <myinput
      msg="密码"
      type="password"
      :proof="/\d{3,9}/"
      errmsg="请输入6到9位密码"
      @getval="setuserpasword"
    ></myinput>
    <!--按钮组件-->
    <mybtn msg="登录" @click.native="axsendlogin"></mybtn>
  </div>
</template>

<script>
// 引入组件
import myinput from "../components/myinput";
import mybtn from "../components/mybtn";
export default {
  components: {
    myinput,
    mybtn,
  },
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    setusername(val) {
      this.username = val;
    },
    setuserpasword(val) {
      this.password = val;
    },

    axsendlogin() {
      if (this.username === "" || this.password === "") {
        this.$toast("内容/密码不能为空");
      } else {
        this.$axios({
          method: "post",
          url: "/login",
          data: {
            username: this.username,
            password: this.password,
          },
        }).then((res) => {
          if (res.data.message === "登录成功") {
            this.$toast(res.data.message);
            // console.log(res.data);
            window.localStorage.setItem("token", res.data.data.token);
            window.localStorage.setItem("tokenid", res.data.data.user.id);
            this.$router.push("/myzoe");
          } else if (res.data.statusCode === 401) {
            this.$toast(res.data.message);
            //不存在后1秒钟跳转
            setTimeout(() => {
              this.$router.push("/register");
            }, 1000);
          }
        });
      }
    },
  },
  computed: {
    // 计算遍历数据返回是true还是false
    // changeisok() {
    //   let isok = true;
    //   // 因为刚开开始的时候为空所以也要判断为的时候改成false
    //   for (let key in this.valobj) {
    //     if (this.valobj[key] == "false" || this.valobj[key] == "") {
    //       isok = false;
    //     }
    //   }
    //   return isok;
    // },
  },
};
</script>

<style lang="less" scoped>
.big_container {
  padding: 16 / 360 * 100vw;
  background-color: #f2f2f2;
  min-height: 100vh;
  box-sizing: border-box;
}
.iconicon-test {
  font-size: 27 / 360 * 100vw;
}
.logo_container {
  text-align: center;
  .iconnew {
    font-size: 126 / 360 * 100vw;
    color: #cc3300;
  }
}
</style>
